<template>
  <div id="updateDialog">
    <el-dialog class="box" title="编辑测点信息" :visible.sync="updateDialogShow" :before-close="handleClose">
      <el-form :model="this.pointData" :rules="rules" ref="this.pointData" label-width="120px" class="demo-form-inline"  >
        <el-form-item label="测点名称："  prop="name" required>
          <el-col :span="23">
            <el-form-item prop="name"  >
              <el-input class="formInput" v-model="pointData.name"   :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="类型：" prop="type" required >
          <el-col :span="9">
            <el-form-item >
              <el-select v-model="pointData.type" placeholder="请选择" :span="24" style="width: 208px">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right"><span class="rightx"></span>地址：</el-col>
          <el-col :span="9">
            <el-form-item >
              <el-input class="formInput"  v-model="pointData.addr" :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="比率：" >
          <el-col :span="9">
            <el-form-item  >
              <el-input class="formInput" v-model="pointData.rates"   :span="24"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right"><span class="rightx"></span>单位：</el-col>
          <el-col :span="9">
            <el-form-item  >
              <el-input class="formInput"  v-model="pointData.unit" :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="显示值：" >
          <el-col :span="9">
            <el-form-item >
              {{pointData.show_data}}
              <span style="color: #fff" v-if="pointData.show_data === ''">1212</span>
              <!--<el-input class="formInput" v-model="pointData.show_data"   :span="24"></el-input>-->
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right"><span class="rightx"></span>原始值：</el-col>
          <el-col :span="9">
            <el-form-item >
              <el-input class="formInput"  v-model="pointData.value" :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="命令1：" >
          <el-col :span="9">
            <el-form-item >
              <el-input class="formInput" v-model="pointData.command1"   :span="24"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right"><span class="rightx"></span>命令2：</el-col>
          <el-col :span="9">
            <el-form-item >
              <el-input class="formInput"  v-model="pointData.command2" :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="命令3：" >
          <el-col :span="9">
            <el-form-item>
              <el-input class="formInput" v-model="pointData.command3"   :span="24"></el-input>
            </el-form-item>
          </el-col>
          <el-col class="line formRight1 userid" :span="5" style="text-align: right"><span class="rightx"></span>格式化：</el-col>
          <el-col :span="9">
            <el-form-item >
              <el-input class="formInput"  v-model="pointData.format" :span="24"></el-input>
            </el-form-item>
          </el-col>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <span class="foorterLeft">注：<span style="color: red">*</span>为必填项</span>
        <el-button type="primary" @click="updatePoint" >确定</el-button>
        <!--<el-button type="primary" @click="updatePoint" v-if="pointData.name === ''" disabled>确定</el-button>-->
        <el-button type="info" @click="handleClose" >取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    props: {
      viewPoint: {},
      Pid: ''
    },
    components: {
    },
    data() {
      return {
        pointData: {},
        pointType: [],
        restart: '更新',
        submitPoint: {
          access_token: localStorage.getItem('access_token'),
          type: 'object_station_get_bycidoid',
          pid: 'default',
          data_applyto: {
            cid: '0001',
            oid: '0001'
          }
        },
        // 获取类型
        submitType: {
          access_token: localStorage.getItem('access_token'),
          type: 'object_station_get_datatype'
        },
        // 修改
        submitUpdate: {
          access_token: localStorage.getItem('access_token'),
          type: 'object_station_update',
          pid: 'default ',
          data: {
            cid: '',
            oid: '',
            name: '',
            value: '',
            unit: '',
            type: '',
            addr: '',
            rates: '',
            show_data: '',
            command1: ' ',
            command2: '5 ',
            command3: '1BYTE ',
            format: '0=否|1=是 '
          }
        },
        rules: {
          name: [
          ],
          type: [
          ]
        },
        options: [],
        updateDialogFalse: false,
        isIndeterminate: true
      }
    },
    created() {
      this.updateDialogShow = true
    },
    mounted() {
      this.queryPointType()
      this.queryPointData()
      console.log(this.viewPoint, this.Pid)
    },
    methods: {
      // 通过cid 和 oid 查询
      // 查询测点信息
      queryPointData() {
        this.submitPoint.data_applyto.cid = this.viewPoint.cid
        this.submitPoint.data_applyto.oid = this.viewPoint.oid
        this.submitPoint.pid = this.Pid
        axios.post('/project', this.submitPoint, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.pointData = res.data.data
          console.log(this.pointData)
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 查询类型
      queryPointType() {
        axios.post('/project', this.submitType, {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        }).then((res) => {
          this.options = res.data.datas
        })
          .catch(function(error) {
            console.log(error)
          })
      },
      // 修改
      updatePoint() {
        if (this.pointData.name === '') {
          this.$message({
            type: 'error',
            message: '测点名称不能为空'
          })
        } else if (this.pointData.type === '') {
          this.$message({
            type: 'error',
            message: '测点类型不能为空'
          })
        } else {
          this.submitUpdate.pid = this.Pid
          this.submitUpdate.data.cid = this.pointData.cid
          this.submitUpdate.data.oid = this.pointData.oid
          this.submitUpdate.data.name = this.pointData.name
          this.submitUpdate.data.value = this.pointData.value
          this.submitUpdate.data.unit = this.pointData.unit
          this.submitUpdate.data.type = this.pointData.type
          this.submitUpdate.data.addr = this.pointData.addr
          this.submitUpdate.data.rates = this.pointData.rates
          this.submitUpdate.data.show_data = this.pointData.show_data
          this.submitUpdate.data.command1 = this.pointData.command1
          this.submitUpdate.data.command2 = this.pointData.command2
          this.submitUpdate.data.command3 = this.pointData.command3
          this.submitUpdate.data.format = this.pointData.format
          axios.post('/project', this.submitUpdate, {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded'
            }
          }).then((res) => {
            if (res.data.errcode === '00000') {
              this.handleClose()
              this.restar()
              this.$message({
                type: 'success',
                message: '编辑成功'
              })
            }
          })
            .catch(function(error) {
              this.$message({
                type: 'success',
                message: error
              })
            })
        }
      },
      // 刷新列表
      restar() {
        this.$emit('diaStatus', this.restart)
      },
      handleCheckAllChange(val) {
        this.isIndeterminate = false
      },
      handleCheckedCitiesChange(value) {
        const checkedCount = value.length
        this.checkAll = checkedCount === this.details.length
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.details.length
      },
      // 子组件传值
      handleClose() {
        const dialogShow = this.updateDialogFalse // 所需要传的值
        this.$emit('giveFalse', dialogShow) // 传值的参数
        console.log(dialogShow)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /*****************第一层弹窗*****************/
  .testColor{
    padding: 0;
    color: #fff;
  }
  .foorterLeft{
    width: 100px;
    height: 20px;
    font-size:14px;
    font-family:MicrosoftYaHei;
    font-weight:400;
    color:rgba(105,114,126,1);
    float: left;
  }
  /*****************第一层弹窗*****************/
  /deep/ .box > .el-dialog{
    width: 750px;
    height: 380px;
    margin-top: 30vh !important;
    border-radius: 4px;
  }
  /*****************第一层弹窗头部*****************/
  /deep/ .box > .el-dialog > .el-dialog__header{
    width: 750px;
    height: 39px;
    line-height: 43px;
    padding: 0;
    text-indent: 20px;
    font-weight: bolder;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    background-color: #e3e6ee;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn{
    top: 12px;
  }
  /deep/ .box > .el-dialog > .el-dialog__header > .el-dialog__headerbtn > i{
    font-size: 18px;
    font-weight: bolder;
  }
  /*****************第一层弹窗内容*****************/
  /deep/ .box > .el-dialog > .el-dialog__body{
    padding: 0 0 0 10px ;
    min-height: 50px;
    line-height: 30px;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item{
    margin-bottom: 0;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__label{
    color: #69727E;
    font-weight: normal;
  }
  /deep/ .box > .el-dialog > .el-dialog__body > .el-form > .el-form-item > .el-form-item__content > .el-input > .el-input__inner{
    width: 610px;
    height: 30px;
  }
  /*****************第一层弹窗底部*****************/
  /deep/ .box > .el-dialog > .el-dialog__footer{
    border-top: 1px solid #e3e6ee;
  }
  /*****************底部按钮*****************/
  /deep/ .dialog-footer{
    width: 100%;
  }
  /deep/ .dialog-footer > .el-button{
    width: 80px;
    height: 30px;
    padding: 0;
  }
  /deep/ .el-input__inner {
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 30px;
    line-height: 30px;
    outline: 0;
    padding: 0 15px;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;
  }
 /deep/ .el-form-item__error {
    color: #f56c6c;
    font-size: 8px;
    line-height: 1;
     padding-top: 4px;
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 10;
   margin-top: -30px;
   margin-left: 20px;
  }
</style>
